package com.masterclient.app.modules.category;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.masterclient.app.R;
import com.masterclient.app.modules.category.adapter.BrandGridAdapter;
import com.masterclient.app.modules.category.beans.CategoryDetailBean;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.widget.NonScrollGridView;

import java.util.List;

/**
 * 作者：sean
 * 创建时间：2017/7/4 17:22
 * 功能说明：
 */

public class SelectorPopWindow extends PopupWindow{


    private List<CategoryDetailBean.ShTopArrBean> list;
    NonScrollGridView gv;
    BrandGridAdapter adapter;
    public SelectorPopWindow(Context context,List<CategoryDetailBean.ShTopArrBean> list) {
        super(context);
        this.list = list;
        View view = View.inflate(context, R.layout.brand_selector,null);
        setContentView(view);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(params);
        setWidth(ScreenUtils.getScreenWidth());
        setHeight(ScreenUtils.getScreenHeight());
        setBackgroundDrawable(null);

        LinearLayout ll_container = view.findViewById(R.id.ll_container);
        gv = view.findViewById(R.id.gv);
        adapter = new BrandGridAdapter(context,list);
        gv.setAdapter(adapter);
        gv.setOnItemClickListener((parent, view1, position, id) -> {
            changeSelect(position);
            dismiss();
            if(onItemClickListerner!=null){
                onItemClickListerner.clickItem(position);
            }
        });

        ll_container.setOnClickListener((ll)->dismiss());
    }

    public void changeSelect(int position) {
        for(int i = 0 ;i<list.size();i++){
            list.get(i).setSelect(false);
            if(position == i){
                list.get(i).setSelect(true);
            }
        }
    }

    @Override
    public void dismiss() {
        if(onItemClickListerner !=null){
            onItemClickListerner.onDismiss();
        }
        super.dismiss();
    }


    public void setList(List<CategoryDetailBean.ShTopArrBean> list) {
        this.list = list;
        adapter.notifyDataSetChanged();
    }

    public void setOnItemClickListerner(OnItemClickListerner onItemClickListerner) {
        this.onItemClickListerner = onItemClickListerner;
    }

    private OnItemClickListerner onItemClickListerner;

    public interface OnItemClickListerner{
        public void clickItem(int position);
        public void onDismiss();
    }


}
